import Flutter
import UIKit
import ZLPhotoBrowser
public class SwiftHelloPlugin: NSObject, FlutterPlugin {
  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "hello_plugin", binaryMessenger: registrar.messenger())
    let instance = SwiftHelloPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    if call.method == "getPlatformVersion"{
        result("iOS " + UIDevice.current.systemVersion)
    }else if call.method == "getImage"{
        self.getImage { images in
            result(["code" : "200","data" : images])
        };
    }
  }
    
    func getImage(callBack:@escaping((_:[Data]) -> Void))  {
        let select = ZLPhotoConfiguration.default();
        select.allowSelectGif = false
        select.allowSelectLivePhoto = false
        select.allowSelectVideo = false
        select.allowTakePhotoInLibrary = false
        let ps = ZLPhotoPreviewSheet(selectedAssets: []);
        ps.showPhotoLibrary(sender: UIViewController.hx_currentController());
        ps.selectImageBlock = {images, assets, isOriginal in
            let datas : [Data] =  images.map { image in
                return image.jpegData(compressionQuality: 1) ?? Data();
            }
            callBack(datas);
        }
    }
}
